package com.ck.framework.security.handle;

import com.ck.common.bean.ResponseResult;
import com.ck.common.utils.json.JacksonUtil;
import com.ck.common.utils.web.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ck
 * @date 2023/3/24 16:06
 * 权限失败处理类 返回权限不足(授权是确定用户能够执行哪些操作或访问哪些资源)
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "权限不足");
        String json = JacksonUtil.obj2json(result);
        WebUtils.renderString(response,json);

    }
}

